<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@ include file="/head.jsp" %>

<body>

<div>
    <div class="layui-form layui-form-pane" style="margin: 5px">

        <input type="hidden" id="videoId" name="videoId" value="${videoId}" />

        <div class="layui-inline" style="float: right">
<%--         <button class="layui-btn" data-type="reload">上传视频</button>--%>
            <div class="layui-input-inline">
                <select name="status" id="status">
                    <option value="">请选择审核状态</option>
                    <option value="0">审核中</option>
                    <option value="1">通过</option>
                    <option value="2">未通过</option>
                </select>
            </div>

            <div class="layui-input-inline">
                <input type="text" name="videoItemName" id="videoItemName"  lay-verify="required"
                       placeholder="请输入视频名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>
    </div>
    <%--    清除上面的浮动对下面布局的影响--%>
    <div style="clear: both;"></div>
    <div class="layui-form layui-form-pane" style="margin: 5px">
        <table id="demo" lay-filter="test"></table>
    </div>
</div>

<%--序号--%>
<script type="text/html" id="increase">
    {{d.LAY_TABLE_INDEX+1}}
</script>

<script type="text/html" id="createTime">
    {{# return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss') }}
</script>

<script type="text/html" id="videoUrl">
<%--    <a href="${url}{{d.videoUrl}}" target="_blank">点击播放</a>--%>
    点击播放
</script>
<script type="text/html" id="videoStatus">
    {{# if (d.videoStatus === 0) { }}
        未审核
    {{# } else if(d.videoStatus === 1) { }}
        通过
    {{# } else if(d.videoStatus === 2) { }}
        不通过
    {{# } else { }}

    {{# } }}
</script>
<script type="text/html" id="isPublish">
    {{# if (d.isPublish=== 0) { }}
        未发布
    {{# } else if(d.isPublish=== 1) { }}
        发布
    {{# } else { }}

    {{# } }}
</script>
<script type="text/html" id="isRecomment">
    {{# if (d.isRecomment=== 0) { }}
        否
    {{# } else if(d.isRecomment=== 1) { }}
        是
    {{# } else { }}

    {{# } }}
</script>


<script type="text/html" id="videoImg">
    <!--使用viewer插件进行渲染-->
    <ul class="showImg">
        {{#  layui.each(d.videoImg.split(','), function(index, item){ }}
        <li><img src="${url}{{item}}" /></li>
        {{#  }); }}
    </ul>
</script>


<script type="text/html" id="barDemo">
    {{# if (d.videoStatus === 0) { }}
        <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
        <a class="layui-btn layui-btn-xs" lay-event="notPass">不通过</a>
    {{# } }}


    <c:if test="${model.glUpdate == 1}">
        {{# if (d.videoStatus === 1) { }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="edit">添加至课程</a>
        {{# } }}

    </c:if>
    <%--<c:if test="${model.glDelete == 1}">--%>
        <%--<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>--%>
    <%--</c:if>--%>
</script>


<script>
    layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,//加载jquery对象
            layer = layui.layer;

        //让层自适应iframe
        $('#add').on('click', function () {
            var index = layer.open({
                title: '信息',
                type: 2,
                content: '/manage/videos/goAdd',
                area: ['800px', '450px'],
                maxmin: true,
                end: function () {
                    location.reload();
                }
            });
            parent.layer.iframeAuto(index);
        });

        var videoId = $("#videoId").val();
        //第一个实例
        table.render({
            elem: '#demo'
            , url: '/manage/videosItem/findAllJson' //数据接口
            , where:{videoId:videoId}
            , page: true //开启分页
            , limit: 10 //初始页条数
            , cols: [[ //表头
                 {field: 'id', title: 'ID', width: 80, templet: '#increase'}
                , {field: 'videoItemName', width: 300, title: '视频名称'}
                , {field: 'videoItemId', hide:true, width: 0, title: '视频Id'}
                , {field: 'videoUrl', title: '视频链接',  width: 100, templet: '#videoUrl',event: 'videoUrl'}
                , {field: 'sort', title: '排序', width:80,sort:true}
                , {field: 'videoName', title: '关联课程名称', width: 150}
                , {field: 'createTime', title: '创建日期', width: 200, templet: '#createTime',sort:true}
                , {field: 'videoStatus', title: '审核状态', width: 150, templet: '#videoStatus',sort:true}
                , {field: 'noPassReason', title: '不通过原因', width: 100}
                , {fixed: 'right', title: '操作栏',  align: 'left', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
            ]]
            , done: function () {
                $(".showImg").viewer();
                $("[data-field='videoItemId']").css('display','none');
                //$("[data-field='userId']").css('display','none');
            }
            , id: 'testReload' //重载标识
        });

        var active = {
            // 方法级渲染的重载 不需要 #
            reload: function () {
                //获取条件
                var videoItemName = $('#videoItemName').val();
                var videoStatus = $('#status').val();

                //执行重载 testReload 对应第一个实例中 id
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        videoItemName: videoItemName,
                        videoStatus:videoStatus
                    }
                });
            }
            // 获取checkStatus行数据
            , dels: function () { //获取选中数据 demo 为table的id
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var userIds = [];
                data.forEach(function (item, index) {
                    userIds.push(item.userId);
                });
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/videosItem/dels',
                        type: 'post',
                        dataType: 'json',
                        data: {userIds: userIds.join(",")},//join 将数组组装成 1,2,3 的形式
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }
            , update: function () { //获取选中数据 demo 为table的id   data
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg("Select an action");
                    return false;
                }
                //data 获取的是数组,所以当修改的时候就要获取第一个数据
                var userId = data[0].userId;
                var index = layer.open({
                    title: '信息',
                    type: 2,
                    content: '/manage/videos/goEdit?userId=' + userId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            }
        };

        $('.layui-form .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'goCheckVideo') { //审核视频
                window.location.href = '/manage/videosItem/findAll?videoId='+data.videoId
            } else if (layEvent === 'videoUrl') { //播放视频
                // var index = layer.open({
                //     title: '播放',
                //     type: 2,
                //     content: '/manage/videosItem/toPlay?videoUrl=' + data.videoUrl,
                //     area: ['800px', '450px'],
                //     maxmin: true,
                //     end: function () {
                //         location.reload();
                //     }
                // });
                // parent.layer.iframeAuto(index);
                window.open( '/manage/videosItem/toPlay?videoUrl=' + data.videoUrl)

            }else if (layEvent === 'pass') { //通过
                layer.confirm('真的通过吗?', function (index) {
                    $.ajax({
                        url: '/manage/videosItem/pass',
                        type: 'post',
                        dataType: 'json',
                        data: {videoItemId: data.videoItemId,videoId:videoId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }else if (layEvent === 'notPass') { //不通过
                console.log(data);
                var index = layer.open({
                    title: '审核',
                    type: 2,
                    content: '/manage/videosItem/toNotPass?videoItemId=' + data.videoItemId +'&videoId=' +  data.videoId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            } else if (layEvent === 'edit') {  //添加到课程
                var index = layer.open({
                    title: '添加到课程',
                    type: 2,
                    content: '/manage/webUser/addToVideo?videoItemId='+data.videoItemId,
                    data:{userId:data.userId},
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            } else if (layEvent === 'openImg') { //监听单元格
                //页面层-展示图片
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: ['450px', '450px'],
                    skin: 'layer-ext-myskin', //没有背景色
                    shadeClose: true,
                    content: '<img src="${url}' + data.userImg + '" height="450px" width="450px" alt="Load failed"/>'
                });
            }
        });

        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                , data = checkStatus.data;
            $.each(data, function (index, item) {
                ids.push(item['userId'])
            });
            return ids;
        }

    });
</script>

<%@ include file="/foot.jsp" %>
